package org.snail.common;

import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class MailUtils {
    private static Logger LOGGER = Logger.getLogger(MailUtils.class);


    /**
     * 发送邮件
     * @param outfilePath 指的是文件的路径
     */
    public static void sendEmail(String outfilePath){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("mail.billjc.com");
        mailSender.setPort(25);
        mailSender.setUsername("gonghebao40277@billjc.com");
        mailSender.setPassword("ghb847514.");

        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);//构造消息helper，第二个参数表明这个消息是multipart类型的
            helper.setFrom("gonghebao40277@billjc.com");
            String[] sendMessagers = {"lilifen36992@billjc.com","wangsisi41970@billjc.com"};
            helper.setTo(sendMessagers);
            helper.setSubject("异常考勤信息");
            helper.setText("详细信息请见下面附件！");
            //使用Spring的FileSystemResource来加载fox.png
            FileSystemResource file = new FileSystemResource(outfilePath);
            helper.addAttachment(outfilePath, file);//添加附加，第一个参数为添加到Email中附件的名称，第二个人参数是图片资源
            mailSender.send(message);
        }catch(MessagingException e){
            LOGGER.error("发送邮件出错:"+e);
        }
    }

}
